package com.itqdkj.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itqdkj.health.dao.SetmealDao;
import com.itqdkj.health.entity.PageResult;
import com.itqdkj.health.entity.QueryPageBean;
import com.itqdkj.health.pojo.Setmeal;
import com.itqdkj.health.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealDao setmealDao;
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        //1.先在中间表中添加数据,关联数据
        setmealDao.add(setmeal);
        setCheckGroupAndCheckItem(setmeal.getId(),checkgroupIds);
    }

    /**
     * 分页查询数据
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> page = setmealDao.selectByCondition(queryPageBean.getQueryString());


        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    /**
     * 根据套餐id查询套餐数据（包含检查组，检查项)
     * @param id
     * @return
     */
    @Override
    public Setmeal findById(int id) {
        return setmealDao.findById(id);
    }

    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return setmealDao.findSetmealCount();
    }

    /**
     * 向中间表中添加数据
     * @param id
     * @param checkgroupIds
     */
    private void setCheckGroupAndCheckItem(Integer id, Integer[] checkgroupIds) {
        for (Integer checkgroupId : checkgroupIds) {
            setmealDao.setCheckGroupAndCheckItem(id,checkgroupId);
        }
    }
}
